package com.hub.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.service.asset.IPartsInfoService;
import com.hub.service.impl.BaseServiceImpl;
import com.hub.dto.asset.PartsInfoDto;
import com.hub.dto.asset.SearchPartsInfoDto;
import com.hub.entity.asset.AssetBrandInfo;
import com.hub.entity.asset.AssetCategoryInfo;
import com.hub.entity.asset.PartsInfoEntity;
import com.hub.frame.consts.ConstantUtil;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.utils.enums.UnitEnum;
import com.hub.mapper.asset.PartsInfoMapper;
import com.hub.model.asset.PartsInfoModel;
import com.hub.service.asset.IAssetBrandInfoService;
import com.hub.service.asset.ICategoryInfoService;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hub
 * @since 2020-05-18
 */
@Service
public class PartsInfoServiceImpl extends BaseServiceImpl<PartsInfoMapper, PartsInfoEntity> implements IPartsInfoService {

    @Autowired
    private Mapper mapper;

    @Autowired
    private IAssetBrandInfoService assetBrandInfoService;

    @Autowired
    private ICategoryInfoService categoryInfoService;

    /**
     * 根据条件查询配件基础信息
     * @param request
     * @return
     */
    public PagingResultObject searchPartsInfoList(SearchPartsInfoDto request)
    {

        request.setPageIndex( request.getPageIndex() > 0 ? request.getPageIndex() : 1);
        request.setPageSize(request.getPageSize() > 0?request.getPageSize():20);

        //查询条件
        QueryWrapper<PartsInfoEntity> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(request.getName())){
            wrapper.like("name", request.getName());
        }
        if(request.getBrandId() > 0){
            wrapper.eq("brand_id", request.getBrandId());
        }
        if(StringUtils.isNotEmpty(request.getMainCategoryCode())){
            wrapper.eq("main_category_code", request.getMainCategoryCode());
        }
        if(StringUtils.isNotEmpty(request.getSmallCategoryCode())){
            wrapper.eq("small_category_code", request.getSmallCategoryCode());
        }
        if(StringUtils.isNotEmpty(request.getModel())){
            wrapper.like("model", request.getModel());
        }
        wrapper.eq("active_flg", ConstantUtil.YES);

        IPage<PartsInfoEntity> result = baseMapper.selectPage(new Page<>(request.getPageIndex(), request.getPageSize()), wrapper);

        List<PartsInfoModel> modelList = new ArrayList<PartsInfoModel>();
        List<PartsInfoEntity> resultList = result.getRecords();
        for (PartsInfoEntity entity: resultList) {
            PartsInfoModel model = mapper.map(entity, PartsInfoModel.class);

            AssetBrandInfo brand = assetBrandInfoService.getById(model.getBrandId());
            if(brand != null) {
                model.setBrandName(brand.getBrandName());
            }

            AssetCategoryInfo mainCategory = categoryInfoService.getMainCategoryByCode(entity.getMainCategoryCode());
            if(mainCategory != null) {
                model.setMainCategoryName(mainCategory.getCategoryName());
            }

            AssetCategoryInfo smallCategory = categoryInfoService.getCategoryByParentCodeAndCode(entity.getMainCategoryCode(), entity.getSmallCategoryCode());
            if(smallCategory != null) {
                model.setSmallCategoryName(smallCategory.getCategoryName());
            }

            modelList.add(model);
        }

        return PagingResultObject.success(modelList,request.getPageIndex(),request.getPageSize(),result.getTotal());
    }

    /**
     *根据ID获取配件基础信息
     * @param id
     * @return
     */
    public ApiResultObject getPartsInfoDetail(Long id) {

        PartsInfoEntity entity = this.getBaseMapper().selectById(id);
        if(entity != null)
        {
            PartsInfoModel model = mapper.map(entity,PartsInfoModel.class);
            //获取名称
            AssetBrandInfo brand = assetBrandInfoService.getById(model.getBrandId());
            if(brand != null) {
                model.setBrandName(brand.getBrandName());
            }

            AssetCategoryInfo mainCategory = categoryInfoService.getMainCategoryByCode(entity.getMainCategoryCode());
            if(mainCategory != null) {
                model.setMainCategoryName(mainCategory.getCategoryName());
            }

            AssetCategoryInfo smallCategory = categoryInfoService.getCategoryByParentCodeAndCode(entity.getMainCategoryCode(), entity.getSmallCategoryCode());
            if(smallCategory != null) {
                model.setSmallCategoryName(smallCategory.getCategoryName());
            }

            if(entity.getUnit() > 0) {
                model.setUnitName(UnitEnum.getDescription((entity.getUnit())));
            }


            return ApiResultObject.success(model);

        }
        return ApiResultObject.failed("未查询到对应配件基础信息");
    }

    /**
     * 新增配件基础信息
     * @param request
     * @return
     */
    public ApiResultObject addPartsInfo(PartsInfoDto request) {
        //检验资产信息是否存在
        QueryWrapper<PartsInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code",request.getCode() ).or().eq("name",request.getName());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("配件主码已存在，无法保存,请重新输入！");
        }

        //保存数据库
        PartsInfoEntity entity = mapper.map(request, PartsInfoEntity.class);
        if(this.save(entity)){
            return ApiResultObject.success("新增配件基础信息成功!");
        }
        return ApiResultObject.failed("配件信息保存数据库失败!");
    }

    /**
     * 修改配件基础信息
     * @param request
     * @return
     */
    public ApiResultObject modifyPartsInfo(PartsInfoDto request) {
        //检验配件信息是否存在
        QueryWrapper<PartsInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code",request.getCode() )
                .eq("id",request.getId())
                .eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) == 0){
            return ApiResultObject.failed("配件不存在，无法保存！");
        }

        //保存数据库
        PartsInfoEntity entity = mapper.map(request, PartsInfoEntity.class);
        if(this.updateById(entity)){
            return ApiResultObject.success("修改配件基础信息成功!");
        }
        return ApiResultObject.failed("配件信息保存数据库失败!");
    }

    /**
     * 删除配件基础信息-逻辑删除
     * @param id
     * @return
     */
    public ApiResultObject removeAssetInfo(Long id) {

        if(id< 1)
            return ApiResultObject.failed("参数异常: 配件信息ID异常!");

        if(this.removeById(id)){
            return ApiResultObject.success("配件基础信息删除成功!");
        }
        return ApiResultObject.failed("配件基础信息异常，删除失败!");
    }


}
